package com.cskaoyan.controller.wx.wxy;


import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.service.wx.wxy.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    /**
     * 查询订单列表
     * @param showType
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo orderList(Integer showType,Integer page,Integer limit){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        return BaseRespVo.ok(wxOrderService.selectOrder(showType,page,limit,marketUser.getId()));
    }

    /**
     *  订单商品详细信息
     * @param orderId
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo orderDetail(Integer orderId){
       return wxOrderService.selectOrderDetail(orderId);
    }


    /**
     *  订单逻辑删除
     * @param map
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int code = wxOrderService.updateOrderDelete(orderId);
        if (code == 1){
            return BaseRespVo.ok("成功");
        }else {
            return BaseRespVo.invalidData();
        }
    }


    /**
     * 修改订单状态  102
     * @param map
     * @return
     */
    @PostMapping("cancel")
    public BaseRespVo orderCancel(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int code = wxOrderService.updateOrderCancel(orderId);
        if (code == 1){
            return BaseRespVo.ok("成功");
        }else {
            return BaseRespVo.invalidData();
        }
    }


    /**
     * 申请退款
     * @param map
     * @return
     */
    @PostMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int code = wxOrderService.updateOrderRefund(orderId);
        if (code == 1){
            return BaseRespVo.ok("成功");
        }else {
            return BaseRespVo.invalidData();
        }
    }


    //http://182.92.235.201:8083/wx/order/goods?orderId=266&goodsId=1068012
    @RequestMapping("goods")
    public BaseRespVo orderGoods(Integer orderId,Integer goodsId){
        return BaseRespVo.ok(wxOrderService.selectOrderGoods(orderId,goodsId));
    }

    //http://182.92.235.201:8083/wx/order/comment
    @PostMapping("comment")
    public BaseRespVo comment(@RequestBody Map map){
        Integer orderGoodsId = (Integer) map.get("orderGoodsId");
        String content = (String) map.get("content");
        Short star = (Short) map.get("star");
        boolean hasPicture = (boolean) map.get("hasPicture");
        String[] picUrls = (String[]) map.get("picUrls");

        int code = wxOrderService.updateComment(orderGoodsId,content,star,hasPicture,picUrls);
        if (code == 1){
            return BaseRespVo.ok("成功");
        }else {
            return BaseRespVo.invalidData();
        }
    }


    @PostMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        BaseRespVo baseRespVo = wxOrderService.confirmOrder(orderId);
        return baseRespVo;
    }


    //http://182.92.235.201:8083/wx/order/prepay
    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
//        int code = wxOrderService.updateOrderStatus(orderId);
        return BaseRespVo.orderNotPayment();
    }


    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody Map map){
        Integer addressId = (Integer) map.get("addressId");
        Integer cartId = (Integer) map.get("cartId");
        Integer couponId = (Integer) map.get("couponId");
        Integer grouponLinkId = (Integer) map.get("grouponLinkId");
        Integer grouponRulesId = (Integer) map.get("grouponRulesId");
        String message = (String) map.get("message");
        Integer userCouponId = (Integer) map.get("userCouponId");

       return BaseRespVo.ok( wxOrderService.insertOrder(addressId,cartId,couponId,grouponLinkId,grouponRulesId,message,userCouponId));
    }
}
